package com.sskj.hangqing.ui.activity;


import android.os.Build;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.LanguageUtil;
import com.sskj.common.widget.DensityWebView;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.bean.HelpBean;
import com.sskj.hangqing.http.HttpConfig;
import com.sskj.hangqing.presenter.GuideWebActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.zzhoujay.richtext.RichText;

import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.HANG_GUIDE_WEB)
public class GuideWebActivity extends BaseActivity<GuideWebActivityPresenter> {
    @BindView(R2.id.tvContent)
    TextView tvContent;
    @BindView(R2.id.llHead)
    LinearLayout llHead;
    @BindView(R2.id.webView)
    DensityWebView webView;

    @Autowired
    String content;
    @Autowired
    String time;
    @Autowired
    String title;
    @Autowired
    String head;
    @Autowired
    boolean isHelp = false;//帮助中心
    @Autowired
    boolean isRule = false;//交易规则
    @Autowired
    boolean isAnti = false;//交易规则
    @Autowired
    boolean isBibi = false;//币币
    @Autowired
    boolean isFabi = false;//fabi
    @Autowired
    boolean isAboutUs = false;//关于我们
    @Autowired
    boolean isZixun = false;//资讯
    @Autowired
    boolean isPrivate = false;//隐私条款
    @BindView(R2.id.tvWebTitle)
    TextView tvWebTitle;
    @BindView(R2.id.tvTime)
    TextView tvTime;
    @Autowired
    boolean isRegisterAgreement = false;//服务协议
    @Autowired
    boolean isHangqingNotice = false;//行情公告
    @Autowired
    boolean isTradeGuide = false;//交易指南
    @Autowired
    boolean isGuset = false;//客服
    @Autowired
    boolean isQuickBuy = false;//快捷买币


    @Override
    protected int getLayoutId() {
        return R.layout.hang_activity_guide_web;
    }

    @Override
    public GuideWebActivityPresenter getPresenter() {
        return new GuideWebActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);


        if (isHelp) {
            setTitle(getString(R.string.hangqingguideWebActivity1));
            llHead.setVisibility(View.GONE);
            tvContent.setVisibility(View.GONE);
            webView.setVisibility(View.VISIBLE);
//            webView.setBackgroundColor(0);
//            webView.getBackground().setAlpha(0);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebChromeClient(new WebChromeClient());
            webView.setWebViewClient(new WebViewClient());
            webView.loadUrl(HttpConfig.BASE_URL + "/help/index.html?language=" + (LanguageUtil.isSimpleChinese() ? "cn" : "eu"));
            webView.setOnKeyListener((v, keyCode, event) -> {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            });
        } else if (isBibi) {
            setTitle(getString(R.string.hangqingguideWebActivity2));
            mPresenter.help("11");
        } else if (isFabi) {
            setTitle(getString(R.string.hangqingguideWebActivity3));
            mPresenter.help("10");
        } else if (isAboutUs) {
            setTitle(getString(R.string.hangqingguideWebActivity4));
            mPresenter.aboutUs();
        } else if (isRegisterAgreement) {
            setTitle(getString(R.string.hangqingguideWebActivity5));
            mPresenter.registerAgreement();
        } else if (isPrivate) {
            setTitle(getString(R.string.hangqingguideWebActivity6));
            mPresenter.privateService();
        } else if (isRule) {
            setTitle(getString(R.string.hangqingguideWebActivity7));
            mPresenter.tradeRule();
        }else if (isAnti) {
            setTitle(getString(R.string.hangqingguideWebActivity8));
            mPresenter.tradeAnti();
        } else if (isZixun) {
            setTitle(getString(R.string.hangqingguideWebActivity9));
            updateText();
        } else if (isHangqingNotice) {
            setTitle(head);
            updateText();
        } else if (isTradeGuide) {
            setTitle(head);
            updateText();
        } else if (isGuset) {
            setTitle(getString(R.string.hangqingguideWebActivity10));
            llHead.setVisibility(View.GONE);
            tvContent.setVisibility(View.GONE);
            webView.setVisibility(View.VISIBLE);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebChromeClient(new WebChromeClient());
            webView.setWebViewClient(new WebViewClient());
            webView.loadUrl("https://www.365webcall.com/chat/ChatWin3.aspx?settings=mw7mbmIN6PwPmmz3A7w07Nz3AmwX070z3AX6mm7N&LL=0");
            webView.setOnKeyListener((v, keyCode, event) -> {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            });
        } else if (isQuickBuy) {
            setTitle(getString(R.string.hangqingguideWebActivity11));
            if (TextUtils.isEmpty(content)) {
                return;
            }
            llHead.setVisibility(View.GONE);
            tvContent.setVisibility(View.GONE);
            initWebView();
            webView.setVisibility(View.VISIBLE);
            webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
        }

    }

    /**
     * 有二级标题时间，WebView展示内容
     */
    private void updateWebView() {
        tvWebTitle.setText(title);
        tvTime.setText(time);
        tvContent.setVisibility(View.GONE);
        webView.setVisibility(View.VISIBLE);
        initWebView();
        content =
                "<html> \n" +
                        "<head> \n" +
                        "<style type=\"text/css\"> \n" +
                        "body {font-size:50px;}\n" +
                        "</style> \n" +
                       " <style>img{width:100% !important;height:auto}</style>"+
                        "</head> \n" +
                        "<body>" +
                        "<script type='text/javascript'>" +
                        "window.onload = function(){\n" +
                        "var $img = document.getElementsByTagName('img');\n" +
                        "for(var p in  $img){\n" +
                        " $img[p].style.width = '100%%';\n" +
                        "$img[p].style.height ='auto'\n" +
                        "}\n" +
                        "}" +
                        "</script>" +
                        content +
                        "</body>" +
                        "</html>";
        webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
    }

    /**
     * 有二级标题时间，TextView展示内容
     */
    public void updateText() {
        if (TextUtils.isEmpty(content)) {
            return;
        }
        webView.setVisibility(View.GONE);
        tvWebTitle.setText(title);
        tvTime.setText(time);

        RichText.from(content).into(tvContent);
    }

    private void initWebView() {
        WebSettings mSettings = webView.getSettings();
        // 支持获取手势焦点
        webView.requestFocusFromTouch();
        webView.setHorizontalFadingEdgeEnabled(true);
        webView.setVerticalFadingEdgeEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        // 支持JS
        mSettings.setJavaScriptEnabled(true);
        mSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mSettings.setBuiltInZoomControls(true);
        mSettings.setDisplayZoomControls(true);
        // 支持插件
        mSettings.setPluginState(WebSettings.PluginState.ON);
        mSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        // 自适应屏幕
        mSettings.setUseWideViewPort(true);
        mSettings.setLoadWithOverviewMode(true);
        // 支持缩放
        mSettings.setSupportZoom(false);//就是这个属性把我搞惨了，
        // 隐藏原声缩放控件
        mSettings.setDisplayZoomControls(false);
        // 支持内容重新布局
        mSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mSettings.supportMultipleWindows();
        mSettings.setSupportMultipleWindows(true);
        // 设置缓存模式
        mSettings.setDomStorageEnabled(true);
        mSettings.setDatabaseEnabled(true);
        mSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        mSettings.setAppCacheEnabled(true);
        mSettings.setAppCachePath(getCacheDir().getAbsolutePath());
        // 设置可访问文件
        mSettings.setAllowFileAccess(true);
        mSettings.setNeedInitialFocus(true);
        mSettings.setBlockNetworkImage(false);
        // 支持自定加载图片
        if (Build.VERSION.SDK_INT >= 19) {
            mSettings.setLoadsImagesAutomatically(true);
        } else {
            mSettings.setLoadsImagesAutomatically(false);
        }
        mSettings.setNeedInitialFocus(true);
        // 设定编码格式
        mSettings.setDefaultTextEncodingName("UTF-8");
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (request.getUrl().toString().equals(HttpConfig.BASE_URL + "/web/index.html")) {
                    finish();
                }
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();

        } else {
            super.onBackPressed();
        }
    }

    public void updateHelp(HelpBean data) {
        llHead.setVisibility(View.GONE);
        if (data != null) {
            content = data.getContent();
        } else {
            content = "";
        }
        updateText();
    }

    public void updatePrivate(HelpBean data) {
        llHead.setVisibility(View.GONE);
        if (data != null) {
            content = data.getContent();
        } else {
            content = "";
        }
        updateText();
    }

    public void updateTradeRule(List<HelpBean> data) {
        llHead.setVisibility(View.GONE);

        if (data != null&&!data.isEmpty()) {
            content = data.get(0).getContent();
        } else {
            content = "";
        }
        updateText();
    }

    public void updateTradeAnti(HelpBean data) {
        llHead.setVisibility(View.GONE);
        if (data != null) {
            content = data.getContent();
        } else {
            content = "";
        }
        updateText();
    }
}
